package com.lg.baselibrary.activity;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.Toolbar;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import com.lg.baselibrary.R;
/**
 * Created by LiGang on 2017/5/29.
 * description：封装了Toolbar的BaseActivity
 */
public abstract class BaseToolbarActivity extends BaseActivity{
    private TextView toolbarTitle;
    private Toolbar toolbar;
    private String menuText;
    private int menuTextRes;
    private int menuIcon;
    private OnToolbarButtonClickListener leftButtonClickListener;
    private OnToolbarButtonClickListener rightButtonClickListener;
    public interface OnToolbarButtonClickListener{
        void onClick();
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_base_toolbar);
        int contentViewLayout = provideContentViewLayout();
        if(contentViewLayout == 0){
            throw new RuntimeException("please provide ContentViewLayout");
        }
        toolbar = findView(R.id.toobar);
        toolbar.setNavigationIcon(R.drawable.selector_toolbar_back);
        toolbarTitle = findView(R.id.toolbar_title);
        View view = LayoutInflater.from(this).inflate(contentViewLayout, null);
        ViewGroup viewGroup = findView(R.id.viewContent);
        viewGroup.addView(view);

        //初始化设置 Toolbar
        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayShowTitleEnabled(false);
        initTitle();
        initView();
        initData();
    }

    protected abstract void initTitle();

    protected void setToolbarTitle(int titleResId){
        this.setToolbarTitle(getString(titleResId));
    }
    protected void setToolbarTitle(CharSequence title){
        this.setToolbarTitle(title,0);
    }
    protected void setToolbarTitle(CharSequence title,int titleColor){
        this.setToolbarTitle(title,titleColor,0);
    }
    protected void setToolbarTitle(CharSequence title,float titleSpSize){
        this.setToolbarTitle(title,0,titleSpSize);
    }
    protected void setToolbarTitle(CharSequence title,int titleColor,float titleSpSize){
        if(!TextUtils.isEmpty(title)){
            toolbarTitle.setText(title);
        }
        if(titleColor != 0){
            toolbarTitle.setTextColor(titleColor);
        }
        if(titleSpSize != 0){
            toolbarTitle.setTextSize(titleSpSize);
        }
    }
    protected void setToolbarLeftButton(int icon){
        setToolbarLeftButton(icon,null);
    }
    protected void setToolbarLeftButton(OnToolbarButtonClickListener listener){
        this.leftButtonClickListener = listener;
    }
    protected void setToolbarLeftButton(int icon,OnToolbarButtonClickListener listener){
        toolbar.setNavigationIcon(icon);
        this.leftButtonClickListener = listener;
    }
    protected void setToolbarRightButton(int menuIcon,OnToolbarButtonClickListener listener){
        this.menuIcon = menuIcon;
        this.rightButtonClickListener = listener;
    }
    protected void setToolbarRightButton(int menuIcon,int menuTextRes,OnToolbarButtonClickListener listener){
        this.setToolbarRightButton(menuIcon,getString(menuTextRes),listener);
    }
    protected void setToolbarRightButton(int menuIcon,String menuText,OnToolbarButtonClickListener listener){
        this.menuIcon = menuIcon;
        this.menuText = menuText;
        this.rightButtonClickListener = listener;
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        if(this.menuIcon != 0){
            getMenuInflater().inflate(R.menu.menu_toolbar,menu);
        }
        return true;
    }

    @Override
    public boolean onPrepareOptionsMenu(Menu menu) {
        if(menuIcon != 0){
            menu.findItem(R.id.menu_toobar).setIcon(menuIcon);
        }
        if(!TextUtils.isEmpty(menuText)){
            menu.findItem(R.id.menu_toobar).setTitle(menuText);
        }
        return super.onPrepareOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if(item.getItemId() == android.R.id.home){
            if(this.leftButtonClickListener != null){
                leftButtonClickListener.onClick();
            }else{
                onBackPressed();
            }
        }else if(item.getItemId() == R.id.menu_toobar){
            rightButtonClickListener.onClick();
        }
        return true;
    }
}
